package com.cm386.final_project_timezonealert.model;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.TimeZone;

public class RegionService {
	public static List<Region> getRegions(){
		List<Region> regions = new ArrayList<Region>();
		String[] timezoneIDs = TimeZone.getAvailableIDs();
		String prevRegion 	 = "";
		boolean isFirst		 = true;
		Arrays.sort(timezoneIDs);
		for (String timezoneID : timezoneIDs){
			String[] regionCity = timezoneID.split("/");
			if (regionCity.length > 1 && !regionCity[0].startsWith("System")){
				if (isFirst){
					prevRegion = regionCity[0];
					isFirst	   = false;
				}
				
				String currentRegion = regionCity[0];
				if (!currentRegion.equalsIgnoreCase(prevRegion)){
					regions.add(new Region(prevRegion, CityService.getCitiesByRegion(prevRegion)));
					prevRegion = currentRegion;
				}
			}
		}
		regions.add(new Region(prevRegion, CityService.getCitiesByRegion(prevRegion)));
		return regions;
	}
	
	public static List<String> getRegionsName(List<Region> regions){
		List<String> regionsName = new ArrayList<String>();
		for (Region region : regions){
			regionsName.add(region.getName());
		}
		return regionsName;
	}
}
